package com.gable.report.controller;

import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.model.ChartsSearchView;
import com.gable.report.model.User;
import com.gable.report.service.ChartsAreaSearchService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 杨青桦
 * @date: 2017-11-1
 * 区报表筛选条件接口
 */
@Controller
@RequestMapping("area/search") /* 建议每个Controller都加上@RequestMapping采用Restful风格URL */
public class ChartsAreaSearchController {
	
	private final Log logger = LogFactory.getLog(this.getClass());

	@Resource
	HttpServletRequest request;
	
	@Resource
	HttpServletResponse response;
	
	@Autowired
	ChartsAreaSearchService chartsAreaSearchService;

	/** 
	 * @Author: 杨青桦
	 * @Description:  查询区辖医院名称列表
	 * @Date: 2017/11/1 13:22
	 * @return
     */
	@RequestMapping(value="/hospital",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil querySearchHospitals(){
		User user = (User)request.getSession().getAttribute("loginUser");
		Map<String, List> map = new HashMap<>();
		try{
			if(user.getGovernmentType() == null){
				return new ReturnResultUtil(0,"该用户不存在");
			}
			List<ChartsSearchView> list = chartsAreaSearchService.queryAreaHospitals(user);
			map.put("list",list);
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}

		return new ReturnResultUtil(1,"查询成功",map);
	}

	/**
	 * @Author: 杨青桦
	 * @Description:  查询所有医院等级名称列表
	 * @Date: 2017/11/1 13:22
	 * @return
	 */
	@RequestMapping(value="/level",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil querySearchHospitalLevels(){
		User user = (User)request.getSession().getAttribute("loginUser");
		Map<String, List> map = new HashMap<>();
		try{
			if(user.getGovernmentType() == null){
				return new ReturnResultUtil(0,"该用户不存在");
			}
			List<ChartsSearchView> list = chartsAreaSearchService.queryHospitalLevels(user);
			map.put("list",list);
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		return new ReturnResultUtil(1,"查询成功",map);
	}

	/**
	 * @Author: 杨青桦
	 * @Description:  查询一级和二级68分类名称列表
	 * @Date: 2017/11/1 13:22
	 * @return
	 */
	@RequestMapping(value="/type",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil querySearchDeviceTypes(){
		User user = (User)request.getSession().getAttribute("loginUser");
		Map<String, Object> map;
		try{
			if(user.getGovernmentType() == null){
				return new ReturnResultUtil(0,"该用户不存在");
			}
			map = chartsAreaSearchService.queryDeviceTypes(user);
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		return new ReturnResultUtil(1,"查询成功",map);
	}

	/**
	 * @Author: 杨青桦
	 * @Description:  查询一级和二级68分类名称列表
	 * @Date: 2017/11/1 13:22
	 * @return
	 */
	@RequestMapping(value="/brand",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil querySearchDeviceBrands(){
		User user = (User)request.getSession().getAttribute("loginUser");
		Map<String, List> map = new HashMap<>();
		try{
			if(user.getGovernmentType() == null){
				return new ReturnResultUtil(0,"该用户不存在");
			}
			List<ChartsSearchView> list = chartsAreaSearchService.queryDeviceBrands(user);
			map.put("list",list);
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		return new ReturnResultUtil(1,"查询成功",map);
	}

	/**
	 * @Author: 杨青桦
	 * @Description:  查询当前年份，当前年份向前推3年（2014-2017）
	 * @Date: 2017/11/1 13:22
	 * @return
	 */
	@RequestMapping(value="/year",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil querySearchCurYears(){
		User user = (User)request.getSession().getAttribute("loginUser");
		Map<String, List> map = new HashMap<>();
		try{
			if(user.getGovernmentType() == null){
				return new ReturnResultUtil(0,"该用户不存在");
			}
			List<ChartsSearchView> list = chartsAreaSearchService.queryCurYears();
			map.put("list",list);
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		return new ReturnResultUtil(1,"查询成功",map);
	}

	/**
	 * @Author: 杨青桦
	 * @Description:  查询医疗设备风险等级分类
	 * @Date: 2017/11/1 13:22
	 * @return
	 */
	@RequestMapping(value="/risk",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil querySearchDeviceRiskLevels(){
		User user = (User)request.getSession().getAttribute("loginUser");
		Map<String, List> map = new HashMap<>();
		try{
			if(user.getGovernmentType() == null){
				return new ReturnResultUtil(0,"该用户不存在");
			}
			List<ChartsSearchView> list = chartsAreaSearchService.queryDeviceRiskLevels();
			map.put("list",list);
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		return new ReturnResultUtil(1,"查询成功",map);
	}

	/** 
	 * @Author: 杨青桦
	 * @Description:  区域设置的默认金额区间
	 * @Date: 2017/11/1 14:10
	 * @return
	 */
	@RequestMapping(value="/price",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil querySearchPriceCategory(){
		User user = (User)request.getSession().getAttribute("loginUser");
		Map<String, List> map = new HashMap<>();
		try{
			if(user.getGovernmentType() == null){
				return new ReturnResultUtil(0,"该用户不存在");
			}
			List<ChartsSearchView> list = chartsAreaSearchService.queryPriceCategory(user);
			map.put("list",list);
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		return new ReturnResultUtil(1,"查询成功",map);
	}


	/** 
	 * @Author: 杨青桦
	 * @Description:  报表截至日期
	 * @Date: 2017/11/1 15:30
	 * @return
	 */
	@RequestMapping(value="/time",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public ReturnResultUtil queryClosingDate(){
		User user = (User)request.getSession().getAttribute("loginUser");
		Map<String, String> map = new HashMap<>();
		try{
			if(user.getGovernmentType() == null){
				return new ReturnResultUtil(0,"该用户不存在");
			}
			Calendar a=Calendar.getInstance();
			int yearNum = a.get(Calendar.YEAR); //获取当前年份
			int monthNum = a.get(Calendar.MONTH)+1; //获取当前月份
			int dayNum = a.get(Calendar.DAY_OF_MONTH); //获取当前日期
			StringBuffer sb = new StringBuffer();
			map.put("time",sb.append(yearNum).append("年").append(monthNum).append("月").append(dayNum).append("日").toString());
			sb.setLength(0);
			map.put("enTime",sb.append(yearNum).append("-").append(monthNum).append("-").append(dayNum).toString());
		}catch(Exception e) {
			e.printStackTrace();
			return new ReturnResultUtil(0,"查询异常");
		}
		return new ReturnResultUtil(1,"查询成功",map);
	}

}
